﻿using Donal.Xms.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.dialog
{
    public partial class dialog_select_coupons : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string cookie_idc = string.Empty;
        protected string status = string.Empty;
        protected string keywords = string.Empty;
        protected string expiration_form = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = 5; //每页数量
            this.keywords = DTRequest.GetQueryString("keywords");
            status = DTRequest.GetQueryString("status");
            expiration_form = DTRequest.GetQueryString("expiration_form");
            
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("coupons", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0 and status=0 " + CombSqlTxt(keywords), "id asc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            ddlExpiration_form.SelectedValue = expiration_form;

            BLL.coupons bll = new BLL.coupons();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();           
            string pageUrl = Utils.CombUrlTxt("dialog_select_coupons.aspx", "expiration_form={0}&keywords={1}&page={2}",
                expiration_form, txtKeywords.Text, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%')");
            }

            if (status != "")
            {
                switch (status)
                {
                    case "z":
                        strTemp.Append(" and status=0");
                        break;
                    case "j":
                        strTemp.Append(" and status=1");
                        break;
                }
            }

            if (expiration_form != "")
            {
                switch (expiration_form)
                {
                    case "zd":
                        strTemp.Append(" and expiration_form=0");
                        break;
                    case "st":
                        strTemp.Append(" and expiration_form=1");
                        break;
                    case "yb":
                        strTemp.Append(" and expiration_form=2");
                        break;
                }
            }


            return strTemp.ToString();
        }
        #endregion

        #region 返回图文每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("dialog_select_coupons_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("dialog_select_coupons_page_size", "Donal.XmsPage", _pagesize.ToString(), 43200);
                }
            }
            Redirect();
        }
        #endregion

        protected void ddlExpiration_form_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        private void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_coupons.aspx", "expiration_form={0}&keywords={1}",
                   ddlExpiration_form.SelectedValue, txtKeywords.Text));
        }
    }
}